package com.liuwei.msmall.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

/**
 * @Classname ElasticSearchConfig
 * @Date 2021/4/10 1:52 下午
 * @Created by llww
 */
@Configuration
public class ElasticSearchConfig {


    public static final RequestOptions COMMON_OPTIONS;

    /**
     * 每次请求elasticsearch所带的请求头
     */
    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
//        builder.addHeader("Authorization", "Bearer " + TOKEN);
//        builder.setHttpAsyncResponseConsumerFactory(
//                new HttpAsyncResponseConsumerFactory
//                        .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));


        COMMON_OPTIONS = builder.build();
    }


    //配置RestHighLevelClient并将器注入到容器中
    @Bean
    public RestHighLevelClient getRestHighLevelClient(){
        RestClientBuilder clientBuilder = RestClient.builder(
                new HttpHost("47.103.210.185", 9200, "http"));
//        clientBuilder.setHttpClientConfigCallback(httpClientBuilder -> {
//            httpClientBuilder.setKeepAliveStrategy((response, context) -> Duration.ofMinutes(5).toMillis());
//            httpClientBuilder.disableAuthCaching();
//            return httpClientBuilder;
//        });
        RestHighLevelClient client = new RestHighLevelClient(clientBuilder);
        return client;
    }

}

